package com.qsq.newsmanager.web;

import com.qsq.newsmanager.dto.InsertExcution;
import com.qsq.newsmanager.entity.News;
import com.qsq.newsmanager.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

/**
 * Created by lining on 2017/3/28.
 */
@Controller
@RequestMapping("/News")
public class NewsController {

    @Autowired
    private NewsService newsService;

    @RequestMapping("/list")
    public String showAll(Model model){
        List<News> list = newsService.queryAll();
        model.addAttribute("list",list);
        return "list";
    }

    @RequestMapping("/{newsId}/detail")
    public String showDetail(@PathVariable("newsId") long newsId,Model model){
        News news = newsService.queryById(newsId);
        model.addAttribute("news",news);
        return "detail";
    }

    @RequestMapping("/writing")
    public String insertNews(@RequestParam(value = "title",required = false) String title,@RequestParam(value = "content",required = false) String content,Model model){
        if(null != title && !"".equals(title)){
            News news = new News();
            news.setTitle(title);
            news.setContent(content);
            news.setCreateTime(new Date());
            InsertExcution insertExcution = newsService.insertNews(news);
            model.addAttribute("insertExcution",insertExcution);
        }
        return "writing";
    }

}
